package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;

import droid.frame.app.Callback;
import droid.frame.utils.http.BitmapUtils;
import droid.frame.view.CodeLinearLayout;

/**
 * 包裹验证
 *
 * @author coffee<br>
 *         2017年6月6日上午11:18:23
 */
public class ParcelCheckActivity extends BaseActivity2 implements View.OnClickListener {

    private CodeLinearLayout mCodeLayout;
    private ScrollView mScrollView;

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_parcel_check);
        super.findViewById();
        getAppTitle().setCommonTitle("取货验证");
        //
        mScrollView = (ScrollView) findViewById(R.id.scrollView);
        final Button submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        setButtonEnableStyle(submit, false);
        //
        final ImageView stationCode = (ImageView) findViewById(R.id.parcel_station_qrcode);
        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... params) {
                UserBean user = AppCache.getUser();
                Bitmap bm = BitmapUtils.loadBitmapFromNet(user.getQrCodeUrl(), true);
                return bm;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                stationCode.setImageBitmap(result);
            }
        }.execute();
        //
        mCodeLayout = (CodeLinearLayout) findViewById(R.id.take_parcel_checkcode_layout);
        mCodeLayout.setCallback(new Callback() {
            @Override
            public void execute(Object... result) {
                //
                mScrollView.smoothScrollTo(0, mScrollView.getBottom());
                setButtonEnableStyle(submit, true);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                String code = mCodeLayout.getText();
                AppHttp.getInstance().takeParcelQueryByCode(code);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.take_parcel_query_byCheckCode:
                final ReqResult<ParcelBean> result = JSON.parseList(msg.obj, ParcelBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (isEmpty(result.getResultList())) {
                                showToast("没有此包裹");
                            } else {
                                Intent intent = new Intent();
                                intent.setClass(getContext(), ParcelCheckListActivity.class);
                                intent.putExtra("items", result.getResultList());
                                startActivityForResult(intent, 101);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 101) {
            setResult(Activity.RESULT_OK);
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
